<template>
  <!-- 更新批注 -->
  <a-modal
    title="批注意见"
    :visible="visible"
    :confirm-loading="loading"
    :width="800"
    @ok="submit"
    @cancel="cancel"
  >
    <a-spin :spinning="loading">
      <a-form-model
        ref="form"
        :model="formData"
        :label-col="labelCol"
        :wrapper-col="wrapperCol"
        :rules="rules"
      >
        <a-form-model-item
          label="批注意见"
          prop="postilSuggestion"
        >
          <BaseTextarea
            v-model.trim="formData.postilSuggestion"
            placeholder="请输入"
          />
        </a-form-model-item>
        <a-form-model-item label="附件">
          <l-table
            v-if="visible"
            :upload-arrys.sync="formData.postilFileList"
            :business-code="businessCode"
          />
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
import { addUpdateLawmanageProgress, editUpdateLawmanageProgress } from '@/api/legal'
import { addComment, editComment } from '@/api/legal/caseAndLetter/caseManage.js'
export default {
  name:'a'+Date.now(),
  props: {
    visible: {
      type: Boolean,
      default: () => false
    },
    form: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      formData: {},
      loading: false,
      businessCode: 'LCM04',
      labelCol: { span: 3 },
      wrapperCol: { span: 21 },
      rules: {
        postilSuggestion: [{ required: true, message: '请输入批注意见', trigger: 'blur' }],
      }
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.formData = this.$clone(this.form)
      } else {
        this.formData = {}
      }
    }
  },
  methods: {
    cancel() {
      this.$refs.form.resetFields()
      this.$emit('cancel')
      this.$emit('update:visible', false)
    },
    submit() {
      this.$refs.form.validate(async (valid) => {
        try {
          if (!valid) return this.$message.warning('信息不完整')
          if (this.loading) return
          this.loading = true
          const { id, executionId, postilSuggestion, postilFileList} = this.formData
          const params = {
              id,
              executionId,
              postilSuggestion,
              postilFileList: postilFileList && postilFileList.map(item => item.commonId) || [],
          }
          const fn = id ? editComment : addComment
          await fn(params)
          this.$message.success('保存成功')
          this.$emit('ok')
        } catch (e) {
          this.$message.warning('保存失败')
        }
        this.loading = false
      })
    }
  }
}
</script>

<style scoped>
</style>